package com.daidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daidai.dao.NewsDao;
import com.daidai.model.News;
import com.daidai.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: Mr.Chen
 * Date: 2022/08/01 17:45
 * Description:
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsDao newsDao;

    @Override
    public News findNewsById(int id) {
        return newsDao.selectById(id);
    }

    @Override
    public List<News> findAllNews() {
        return newsDao.selectList(null);
    }

    @Override
    public List<News> findLastNews() {
        Page<News> page = new Page<>(1, 5);
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(News::getCreateTime);
        Page<News> newsPage = newsDao.selectPage(page, lambdaQueryWrapper);
        return newsPage.getRecords();
    }

    @Override
    public void addNews(String title, String description, String createTime) {
        newsDao.insert(new News().setTitle(title).setDescription(description).setCreateTime(createTime));
    }

    @Override
    public void deleteNews(int id) {
        newsDao.deleteById(id);
    }

    @Override
    public void updateNews(String title, String description, int id) {
        newsDao.updateById(new News().setId(id).setTitle(title).setDescription(description));
    }
}
